package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sport;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodDao foodDao;

    /**
     *
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Sport> page = foodDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 新增食物信息
     * @param food
     */
    public void addFood(Food food) {
        foodDao.addFood(food);

    }

    /**
     * 根据id查询食物信息
     * @param id
     * @return
     */
    public Food findFoodById(Integer id) {
        return foodDao.findFoodById(id);
    }

    /**
     * 编辑食物信息
     * @param food
     */
    public void editFood(Food food) {
        foodDao.editFood(food);

    }

    /**
     *
     * 根据id删除食物信息
     * @param id
     */
    public void deleteFoodById(Integer id) {
        foodDao.deleteFoodById(id);

    }

    /**
     * 批量导入食物数据
     * @param data
     */
    public void insertFood(List<Food> data) {
        for (Food food : data) {
            foodDao.insertFood(food);
        }

    }
}
